package com.sergio.compress.service;

import com.sergio.compress.cp.base.CompressResult;
import com.sergio.compress.cp.base.CompressTest;
import com.sergio.compress.cp.base.DecompressResult;
import com.sergio.compress.cp.base.RandomSource;
import com.sergio.compress.cp.mainUtil.LZ4;
import com.sergio.compress.cp.mainUtil.SNAPPY;
import com.sergio.compress.cp.mainUtil.ZLIB;
import com.sergio.compress.cp.mainUtil.ZSTD;
import com.sergio.compress.dao.model.CpTestRecord;
import com.sergio.compress.dao.service.CpTestRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.DataFormatException;

/**
 * @Author : Sergio.D
 * @Description :
 * @CreateDate ：2023/6/15 10:31
 */
@Service
public class CompressTestService {

    @Autowired
    CpTestRecordService cpTestService;

    private static boolean needSave = false;

    final String TEST_FILE_PATH = "files/dickens";

    public void doTest(int start, int max, int step) throws IOException, DataFormatException {
        File file = new File(TEST_FILE_PATH);
        byte[] sourceData = Files.readAllBytes(file.toPath());
        int maxSize = 1024 * 1024;
//        int maxSize = 200;
        for (int size = start; size <= max; size += step) {
            byte[] byteArray = RandomSource.randomByte(sourceData, size);
            // 在这里进行你的测试
//            System.out.println("  size : " + byteArray.length + " data : " + new String(byteArray));
//            System.out.println("  size : " + byteArray.length);
            //跳过第一轮  方式冷启动导致的数据过大
            makeTest(byteArray);
        }
    }

    public List<CpTestRecord> makeTest(byte[] dataSource) throws DataFormatException, IOException {
        List<CpTestRecord> result = new ArrayList<>();

        result.add(saveCpTestResult(new LZ4().test(dataSource), needSave));

        result.add(saveCpTestResult(new ZLIB().test(dataSource), needSave));

        result.add(saveCpTestResult(new ZSTD().test(dataSource), needSave));
//        result.add(saveCpTestResult(new ZSTD(1).test(dataSource), needSave));
//        result.add(saveCpTestResult(new ZSTD(2).test(dataSource), needSave));
//        result.add(saveCpTestResult(new ZSTD(6).test(dataSource), needSave));
//        result.add(saveCpTestResult(new ZSTD(12).test(dataSource), needSave));
//        result.add(saveCpTestResult(new ZSTD(17).test(dataSource), needSave));
//        result.add(saveCpTestResult(new ZSTD(22).test(dataSource), needSave));

        result.add(saveCpTestResult(new SNAPPY().test(dataSource), needSave));
//        this.needSave = true;
        return result;
    }

    public CpTestRecord saveCpTestResult(CompressResult compressResult, DecompressResult decompressResult, boolean needSave) {
        CpTestRecord cpTest = new CpTestRecord()
                .setLengthSe((long) compressResult.getLengthSource())
                .setLengthCp((long) compressResult.getLengthCompressed())
                .setResultCp(decompressResult.isResult())
                .setType(compressResult.getType())
                .setCostTimeCp(compressResult.getCostTime())
                .setCostTimeDcp(decompressResult.getCostTime())
                .setTime(new Date())
                .makeCpRate();
        if (needSave) {
            cpTestService.save(cpTest);
        }
        return cpTest;
    }

    public CpTestRecord saveCpTestResult(CompressTest compressTest) {
        return saveCpTestResult(compressTest.getCompressResult(), compressTest.getDecompressResult(), true);
    }

    public CpTestRecord saveCpTestResult(CompressTest compressTest, boolean needSave) {
        return saveCpTestResult(compressTest.getCompressResult(), compressTest.getDecompressResult(), needSave);
    }


}
